package vip.xiaonuo.smzq.core.tools.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;

public class DynamicTitleWriteHandler implements CellWriteHandler {

    private final String dynamicTitle; // 动态标题

    public DynamicTitleWriteHandler(String dynamicTitle) {
        this.dynamicTitle = dynamicTitle;
    }

    public void beforeCellCreate(AnalysisContext context, WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) { // 如果是表头
            // 获取当前列的索引
            int columnIndex = head.getColumnIndex();
            // 根据索引判断是否需要修改标题
            if (columnIndex == 6) { // 第七个表头
                // 替换表头
                writeSheetHolder.getSheet().getRow(0).getCell(columnIndex).setCellValue(dynamicTitle);
            }
        }
    }

    // 其他方法可以按需实现
}